package flirtme.activities;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;

import com.ans.flirtme.R;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import flirtme.constants.Constants;

public class MapActivity extends Activity {

	private static double LAT_BSAS = -34.6;
	private static double LONG_BSAS = -58.43333;
	private static int MARKER_ZOOM = 13;

	private GoogleMap _map = null;
	private GoogleMapOptions _mapOptions = null;
	private MapFragment _mapFragment = null;
	private Boolean _mapsAvailable = false;

	private double _latitude;
	private double _longitude;
	private String _positionText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);

		// Get position data
		Bundle b = getIntent().getExtras();
		_latitude = b.getDouble(Constants.LATITUDE_KEY);
		_longitude = b.getDouble(Constants.LONGITUDE_KEY);
		_positionText = b.getString(Constants.POSITION_TEXT_KEY);

		createMapOptions();
		createMapFragment();
	}

	@Override
	protected void onStart() {
		super.onStart();
		setUpMapInstance();
		setMapPosition(_latitude, _longitude, MARKER_ZOOM);
		addNewMarker(_latitude, _longitude, "Lo de Ramita");
	}

	private void createMapOptions() {
		_mapOptions = new GoogleMapOptions();
	}

	private void setMapPosition(double latitude, double longitude, int zoom) {
		LatLng position = new LatLng(latitude, longitude);
		if (_mapsAvailable)
			_map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, zoom));
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

	public void addNewMarker(double latitude, double longitude, String text) {
		if (_mapsAvailable)
			_map.addMarker(new MarkerOptions().position(
					new LatLng(latitude, longitude)).title(text));
	}

	private void createMapFragment() {
		// Create map fragment
		_mapFragment = MapFragment.newInstance(_mapOptions);
		FragmentTransaction fragmentTransaction = getFragmentManager()
				.beginTransaction();
		fragmentTransaction.add(R.id.map_layout, _mapFragment);
		fragmentTransaction.commit();

	}

	private void setUpMapInstance() {
		// Do a null check to confirm that we have not already instantiated the
		// map.
		if (_map == null) {
			_map = _mapFragment.getMap();

			// Check if we were successful in obtaining the map.
			if (_map != null) {
				// The Map is verified. It is now safe to manipulate the map.
				_mapsAvailable = true;
				try {
					MapsInitializer.initialize(this);
				} catch (GooglePlayServicesNotAvailableException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}